

public class avlTree {
    public class TreeNode {
        public int val;
        public int bf;

        public TreeNode left;
        public TreeNode right;
        public TreeNode parent;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    public TreeNode root;

    public boolean insert(int val) {
        TreeNode node = new TreeNode(val);
        if (root == null) {
            root = node;
            return true;
        }

        TreeNode parent = null;
        TreeNode cur = root;
        while (cur != null) {
            if (cur.val < val) {
                parent = cur;
                cur = cur.right;
            } else if (cur.val == val) {
                return false;
            } else {
                parent = cur;
                cur = cur.left;
            }
        }

        if (parent.val > val) {
            parent.left = node;
        } else {
            parent.right = node;
        }
        node.parent = parent;

        cur = node;

        //调节平衡因子
        while (parent != null) {
            if (cur == parent.right) {
                parent.bf++;
            } else {
                parent.bf--;
            }

            if (parent.bf == 0) {
                break;
            } else if (parent.bf == 1 || parent.bf == -1) {
                //继续向上判断
                cur = parent;
                parent = parent.parent;
            } else {
                if (parent.bf == 2) { //右树高 需要降低右树的高度
                    if (cur.bf == 1) {
                        //左旋
                        rotateLeft(parent);
                    } else {
                        //cur.bf == -1
                        rotateRL(parent);
                    }
                } else {
                    //parent.bf == -2 左树高 需要降低左树的高度
                    if (cur.bf == -1) {
                        rotateRight(parent);
                    } else {
                        //cur.bf == 1
                        //右旋
                        rotateLR(parent);
                    }
                }
                break;
            }
        }

        return true;
    }

    private void rotateRL(TreeNode parent) {
        TreeNode subR = parent.right;
        TreeNode subRL = subR.left;
        int bf = subRL.bf;

        rotateRight(parent.right);
        rotateLeft(parent);

        if(bf == 1) {
            parent.bf = -1;
            subR.bf = 0;
            subRL.bf = 0;
        }else if(bf == -1) {
            parent.bf = 0;
            subR.bf = 0;
            subRL.bf = 1;
        }
    }

    private void rotateLR(TreeNode parent) {
        TreeNode subL = parent.left;
        TreeNode subLR = subL.right;
        int bf = subLR.bf;

        rotateLeft(parent.left);
        rotateRight(parent);

        if(bf == -1) {
            subL.bf = 0;
            subLR.bf = 0;
            parent.bf = 1;
        }else if(bf == 1) {
            subL.bf = -1;
            subLR.bf = 0;
            parent.bf = 0;
        }
    }

    private void rotateLeft(TreeNode parent) {
        TreeNode subR = parent.right;
        TreeNode subRL = subR.left;
        TreeNode pParent = parent.parent;

        parent.right = subRL;
        if (subRL != null) {
            subRL.parent = parent;
        }
        subR.left = parent;
        parent.parent = subR;

        if(parent == root) {
            root = subR;
            root.parent = null;
        }else {
            if(pParent.left == parent) {
                pParent.left = subR;
            }else {
                pParent.right = subR;
            }
            subR.parent = pParent;
        }
        subR.bf = parent.bf = 0;
    }

    private void rotateRight(TreeNode parent) {
        TreeNode subL = parent.left;
        TreeNode subLR = subL.right;
        TreeNode pParent = parent.parent;

        subL.right = parent;
        parent.parent = subL;
        parent.left = subLR;
        if (subLR != null) {
            subLR.parent = parent;
        }
        if (parent == root) {
            subL.parent = null;
            root = subL;
        } else {
            if (pParent.left == parent) {
                pParent.left = subL;
            } else {
                pParent.right = subLR;
            }
            subL.parent = pParent;
        }


        subL.bf = parent.bf = 0;
    }

}
